<?php


namespace App\Modules\Health\Http\Controllers\Admin;


use App\Modules\Health\Constants\AcuConfigConstant;
use App\Modules\Health\Constants\AdvConfigConstant;
use App\Modules\Health\Models\HealthConfigs;
use App\Modules\Health\Services\ConfigService;
use Dcat\Admin\Form;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class ConfigController extends AdminController
{
    public function index(Content $content)
    {
        $content->row(function (Row $row) {
            $tab = new Tab();

            $tab->add('首页轮播图设置', $this->banner());
            $tab->add("广告设置", $this->adv());
            $tab->add('分类展示样式', $this->class());

            $row->column(12, $tab->withCard());
        });

        return $content->header("穴位大全配置");
    }

    protected function banner()
    {
        $form = new Form();

        $form->action(admin_route('health.configs.store'));

        $configs = app(ConfigService::class)->getConfigs(AdvConfigConstant::BANNER_GROUP);

        $form->hidden('slug')->value('banner');
        $form->multipleImage(AdvConfigConstant::BANNER_GROUP, '轮播图')
            ->sortable()
            ->url('upload/files')
            ->autoUpload()
            ->customFormat(function ($v) {
                return $v;
            })->limit(9)
            ->help('最多添加9张banner图')
            ->value($configs[AdvConfigConstant::BANNER_GROUP] ?? '');

        $form->disableHeader();
        $form->disableResetButton();
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();

        return $form;
    }

    protected function adv()
    {
        $form = new Form();

        $configs = app(ConfigService::class)->getConfigs(AdvConfigConstant::GROUP);

        $form->action(admin_route('health.configs.store'));


        $form->block(12, function (Form\BlockForm $form) use ($configs) {
            $form->title('广告位设置');
            $form->switch(AdvConfigConstant::INDEX_STATUS, '首页广告开关')->default(0)->value($configs[AdvConfigConstant::INDEX_STATUS] ?? 0);
            $form->switch(AdvConfigConstant::ACU_STATUS, '穴位详情页广告开关')->default(0)->value($configs[AdvConfigConstant::ACU_STATUS] ?? 0);
            $form->switch(AdvConfigConstant::DISEASE_STATUS, '病症详情页广告开关')->default(0)->value($configs[AdvConfigConstant::DISEASE_STATUS] ?? 0);
            $form->switch(AdvConfigConstant::BREATHE_LIST_STATUS, '爱呼吸列表页广告开关')->default(0)->value($configs[AdvConfigConstant::BREATHE_LIST_STATUS] ?? 0);
            $form->switch(AdvConfigConstant::BREATHE_STATUS, '爱呼吸详情页广告开关')->default(0)->value($configs[AdvConfigConstant::BREATHE_STATUS] ?? 0);
        });

        $form->block(12, function (Form\BlockForm $form) {
            $form->hidden('slug')->value('adv');
            $form->submitButton();
        });


        $form->disableHeader();
        $form->disableResetButton();
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();

        return $form;
    }

    protected function class()
    {
        $form = new Form();

        $configs = app(ConfigService::class)->getConfigs(AcuConfigConstant::ACU_GROUP);

        $form->hidden('slug')->value('class');
        $form->radio('acu_class_type', '穴位分类展示样式')->options(AcuConfigConstant::ACU_CLASS_TYPE_MAPS)->default(0)->value($configs[AcuConfigConstant::SHOW_ACU_CLASS_TYPE] ?? 0);

        $form->action(admin_route('health.configs.store'));
        $form->disableHeader();
        $form->disableResetButton();
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();

        return $form;
    }

    public function store()
    {
        $post = request()->post();
        $slug = $post['slug'] ?? '';

        if (!$slug) {
            return JsonResponse::make()->error('参数错误');
        }

        try {
            $method = 'store' . Str::studly($slug);
            $this->{$method}($post);
        } catch (\Throwable $e) {
            return JsonResponse::make()->error('保存失败');
        }

        return JsonResponse::make()->success('保存成功');
    }

    protected function storeBanner(array $data)
    {
        $group = AdvConfigConstant::BANNER_GROUP;

        $banners = $data[AdvConfigConstant::BANNER_GROUP] ?? '';

        if ($banners) {
            $banners = explode(',', $banners);
            $banners = array_map(function ($item) {
                if (!Str::startsWith($item, ['http://', 'https://', '://'])) {
                    $item = Storage::disk(config('admin.upload.disk'))->url($item);
                }
                return $item;
            }, $banners);
            $banners = implode(',', $banners);
        }

        HealthConfigs::updateOrCreate([
            'group' => $group,
            'name'  => $group,
        ], ['value' => $banners]);

        app(ConfigService::class)->flushConfigs($group);
    }

    protected function storeAdv(array $data)
    {
        $data = Arr::only($data, [
            AdvConfigConstant::INDEX_STATUS,
            AdvConfigConstant::ACU_STATUS,
            AdvConfigConstant::DISEASE_STATUS,
            AdvConfigConstant::BREATHE_LIST_STATUS,
            AdvConfigConstant::BREATHE_STATUS,
        ]);

        foreach ($data as $key => $item) {
            HealthConfigs::updateOrCreate([
                'group' => AdvConfigConstant::GROUP,
                'name'  => $key,
            ])->update(['value' => $item]);
        }

        app(ConfigService::class)->flushConfigs(AdvConfigConstant::GROUP);
    }

    protected function storeClass(array $data)
    {
        $data = Arr::only($data, [
            AcuConfigConstant::SHOW_ACU_CLASS_TYPE,
        ]);

        foreach ($data as $key => $item) {
            HealthConfigs::updateOrCreate([
                'group' => AcuConfigConstant::ACU_GROUP,
                'name'  => $key,
            ])->update(['value' => $item]);
        }

        app(ConfigService::class)->flushConfigs(AcuConfigConstant::ACU_GROUP);
    }
}
